package conf2;

import conf2.ui.FontSet;
import conf2.ui.Images;
import conf2.ui.MainMenu;
import conf2.ui.MainWindow;
import conf2.ui.Portraits;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class Application implements Runnable
{

    public static final String VERSION = "2.0.0";
    public static final String APP_NAME = "Confrontation v" + VERSION;
    private MainWindow mw;
    private MainMenu menu;
    private MenuListener ml;
    private Configuration config;

    public Application()
    {
    }

    public static void main(String[] args) throws Exception
    {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            // whatever
        }

        SwingUtilities.invokeLater(new Application());
    }

    public void run()
    {

        config = new Configuration();
        try {
            String path = Configuration.getProperFilePath();
            System.out.println("Trying to load configuration from:  " + path);
            config.load(path);
        } catch (IOException e) {
            System.err.println("  -- I/O Error, will create a default configuration and try to save it later.");
        } catch (Configuration.CorruptedException ce) {
            System.err.println("  -- Configuration file was corrupted, will recreate a default configuration.");
        }

        Portraits.preload();
        FontSet.preload();
        Images.preload();

        menu = new MainMenu(config);

        mw = new MainWindow();
        ml = new MenuListener(mw, config);
        menu.setMenuListener(ml);

        mw.setJMenuBar(menu);

        mw.setTitle(APP_NAME);
        mw.addWindowListener(new WindowAdapter()
        {

            @Override
            public void windowClosing(WindowEvent e)
            {
                ml.gameExit();
            }
        });

        GameStarter starter = new GameStarter(mw, menu, config);
        ml.setGameStarter(starter);

        mw.pack();
        mw.setVisible(true);
    }
}